package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.util.KeysUtil;
import org.springframework.lang.Nullable;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class CookieUtil {

    public static void addCookie(HttpServletResponse response, Cookie cookie){
        cookie.setMaxAge(KeysUtil.USER_LOGIN_TOKEN.getTime().intValue()*60);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public static String getTokenByCookie(@Nullable HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    String token = cookie.getValue();
                    return token;
                }
            }
        }
        return null;
    }
}
